package com.eaoa.uc.busorder.action;

import java.util.Collections;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.eaoa.base.EABaseAction;
import com.eaoa.constants.AttachmentConstants;
import com.eaoa.constants.PubLogConstants;
import com.eaoa.query.bus.BusOrderQuery;
import com.eaoa.query.pub.PubBilllogQuery;
import com.eaoa.utils.AttachmentUtils;
import com.eaoa.vo.bus.BusOrder;
import com.eaoa.vo.pub.PubBilllog;
import com.stony.core.util.CoreUtils;

public class BusOrderManageSearchExpandAction extends EABaseAction{

	protected ActionForward doWork(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		Long orderno = CoreUtils.parseLong(request.getParameter("orderno"));
		
		BusOrder order = BusOrderQuery.loadByNo(orderno);
		
		request.setAttribute("result", order);
		
		if(orderno!=null){
			Map attachments = AttachmentUtils.getAttachmentMap(AttachmentConstants.ATTACHMENTTYPE_ORDER,orderno);
			request.setAttribute("attachments", attachments);
		}
		
		if(order.getBusTransfer()!=null){
			List<PubBilllog> orderlogs = PubBilllogQuery.load(orderno, 
					PubLogConstants.BILLTYPE_PUB_ORDER, PubLogConstants.LOGTYPE_STATUS, "Y");
			
			List<PubBilllog> transferlogs = PubBilllogQuery.load(order.getBusTransfer().getTransferno(), 
					PubLogConstants.BILLTYPE_PUB_TRANSFER, PubLogConstants.LOGTYPE_BILLTRACE, "Y");
			
			orderlogs.addAll(transferlogs);
			Collections.sort(orderlogs);
			
			request.setAttribute("billlogs", orderlogs);
		}
		
		String isSelf = "N";
		if(order.getCreatorno().equals(getEmployee(request).getAccountno())){
			isSelf = "Y";
		}
		request.setAttribute("isSelf", isSelf);
		
		return mapping.findForward("success");
	}
}